// ===== 本地存储工具函数 =====

/**
 * 安全地获取 sessionStorage 中的 JSON 数据
 * @param key 存储键名
 * @param defaultValue 默认值
 * @returns 解析后的数据或默认值
 */
export const getSessionItem = <T = any>(key: string, defaultValue: T | null = null): T | null => {
  try {
    const item = sessionStorage.getItem(key)
    if (item === null) return defaultValue
    return JSON.parse(item) as T
  } catch (error) {
    console.warn(`Failed to parse sessionStorage item "${key}":`, error)
    return defaultValue
  }
}

/**
 * 安全地设置 sessionStorage 中的 JSON 数据
 * @param key 存储键名
 * @param value 要存储的值
 */
export const setSessionItem = <T = any>(key: string, value: T): void => {
  try {
    sessionStorage.setItem(key, JSON.stringify(value))
  } catch (error) {
    console.error(`Failed to set sessionStorage item "${key}":`, error)
  }
}

/**
 * 安全地获取 localStorage 中的 JSON 数据
 * @param key 存储键名
 * @param defaultValue 默认值
 * @returns 解析后的数据或默认值
 */
export const getLocalItem = <T = any>(key: string, defaultValue: T | null = null): T | null => {
  try {
    const item = localStorage.getItem(key)
    if (item === null) return defaultValue
    return JSON.parse(item) as T
  } catch (error) {
    console.warn(`Failed to parse localStorage item "${key}":`, error)
    return defaultValue
  }
}

/**
 * 安全地设置 localStorage 中的 JSON 数据
 * @param key 存储键名
 * @param value 要存储的值
 */
export const setLocalItem = <T = any>(key: string, value: T): void => {
  try {
    localStorage.setItem(key, JSON.stringify(value))
  } catch (error) {
    console.error(`Failed to set localStorage item "${key}":`, error)
  }
}

/**
 * 移除存储项
 * @param key 存储键名
 * @param type 存储类型
 */
export const removeItem = (key: string, type: 'session' | 'local' = 'local'): void => {
  try {
    if (type === 'session') {
      sessionStorage.removeItem(key)
    } else {
      localStorage.removeItem(key)
    }
  } catch (error) {
    console.error(`Failed to remove ${type}Storage item "${key}":`, error)
  }
}

/**
 * 清空存储
 * @param type 存储类型
 */
export const clearStorage = (type: 'session' | 'local' = 'local'): void => {
  try {
    if (type === 'session') {
      sessionStorage.clear()
    } else {
      localStorage.clear()
    }
  } catch (error) {
    console.error(`Failed to clear ${type}Storage:`, error)
  }
}

// 常用的业务存储函数
/**
 * 获取网站信息
 */
export const getWebsiteInfo = () => {
  return getSessionItem('website', null)
}

/**
 * 设置网站信息
 */
export const setWebsiteInfo = (website: any) => {
  setSessionItem('website', website)
}

/**
 * 获取用户偏好设置
 */
export const getUserPreferences = () => {
  return getLocalItem('user-preferences', {
    theme: 'auto',
    language: 'zh-CN',
    fontSize: 'medium',
    animations: true
  })
}

/**
 * 设置用户偏好
 */
export const setUserPreferences = (preferences: any) => {
  setLocalItem('user-preferences', preferences)
}

/**
 * 获取阅读历史
 */
export const getReadingHistory = (): string[] => {
  return getLocalItem('reading-history', [])
}

/**
 * 添加到阅读历史
 */
export const addToReadingHistory = (articleId: string) => {
  const history = getReadingHistory()
  const newHistory = [articleId, ...history.filter(id => id !== articleId)].slice(0, 50)
  setLocalItem('reading-history', newHistory)
}

/**
 * 获取收藏列表
 */
export const getFavorites = (): string[] => {
  return getLocalItem('favorites', [])
}

/**
 * 切换收藏状态
 */
export const toggleFavorite = (articleId: string): boolean => {
  const favorites = getFavorites()
  const index = favorites.indexOf(articleId)
  
  if (index > -1) {
    favorites.splice(index, 1)
    setLocalItem('favorites', favorites)
    return false
  } else {
    favorites.unshift(articleId)
    setLocalItem('favorites', favorites)
    return true
  }
}